using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
	class UserCultureProfileDA {

		private const string SP_GetAll = "dbo.UserCultureProfile_GetAll";
		private const string SP_GetByUserKey = "dbo.UserCultureProfile_GetByUserKey";
		private const string SP_Insert = "dbo.UserCultureProfile_Insert";
		private const string SP_Update = "dbo.UserCultureProfile_Update";

		public static UserCultureProfileEnt CreateFromReader(IDataReader reader) {
			UserCultureProfileEnt item = new UserCultureProfileEnt();
			item.UserKey = ConvertUtils.ToString(reader["UserKey"]);
			item.NationalityID = ConvertUtils.ToShort(reader["NationalityID"]);
			item.Nationality = ConvertUtils.ToString(reader["Nationality"]);
			item.EthnicID = ConvertUtils.ToShort(reader["EthnicID"]);
			item.Ethnic = ConvertUtils.ToString(reader["Ethnic"]);
			item.LanguageIDs = ConvertUtils.ToString(reader["LanguageIDs"]);
			//item.Languages = ConvertUtils.ToString(reader["Languages"]);
			item.ReligionID = ConvertUtils.ToShort(reader["ReligionID"]);
			item.Religion = ConvertUtils.ToString(reader["Religion"]);
			item.LunarYearID = ConvertUtils.ToShort(reader["LunarYearID"]);
			item.LunarYear = ConvertUtils.ToString(reader["LunarYear"]);
			item.ZodiacMonthID = ConvertUtils.ToShort(reader["ZodiacMonthID"]);
			item.ZodiacMonth = ConvertUtils.ToString(reader["ZodiacMonth"]);
			item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
			item.DateModified = ConvertUtils.ToDateTime(reader["DateModified"]);
			return item;
		}

        public static UserCultureProfileEnt GetAll(string userKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			UserCultureProfileEnt result = null;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetAll)) {
				db.AddParameter(cmd, "UserKey", userKey);
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					if (reader.Read()) {
						result = CreateFromReader(reader);
					}
				}
			}
			return result;
		}

        public static UserCultureProfileEnt GetByUserKey(string userKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			UserCultureProfileEnt result = null;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetByUserKey)) {
				db.AddParameter(cmd, "UserKey", userKey);
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					if (reader.Read()) {
						result = CreateFromReader(reader);
					}
				}
			}
			return result;
		}

		public static int Insert(UserCultureProfileEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
				db.AddParameter(cmd, "UserKey", item.UserKey);
				db.AddParameter(cmd, "NationalityID", item.NationalityID);
				db.AddParameter(cmd, "EthnicID", item.EthnicID);
				db.AddParameter(cmd, "LanguageIDs", item.LanguageIDs);
				db.AddParameter(cmd, "ReligionID", item.ReligionID);
				db.AddParameter(cmd, "LunarYearID", item.LunarYearID);
				db.AddParameter(cmd, "ZodiacMonthID", item.ZodiacMonthID);
				db.AddParameter(cmd, "DateCreated", item.DateCreated);
				db.AddParameter(cmd, "DateModified", item.DateModified);

				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

        public static int Update(UserCultureProfileEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Update)) {
				db.AddParameter(cmd, "UserKey", item.UserKey);
				db.AddParameter(cmd, "NationalityID", item.NationalityID);
				db.AddParameter(cmd, "EthnicID", item.EthnicID);
				db.AddParameter(cmd, "LanguageIDs", item.LanguageIDs);
				db.AddParameter(cmd, "ReligionID", item.ReligionID);
				db.AddParameter(cmd, "LunarYearID", item.LunarYearID);
				db.AddParameter(cmd, "ZodiacMonthID", item.ZodiacMonthID);
				db.AddParameter(cmd, "DateCreated", item.DateCreated);
				db.AddParameter(cmd, "DateModified", item.DateModified);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

	}
}